﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using control.Biz;

namespace control
{
    public partial class frmChon : Form
    {
        private BizSach selected;
        public BizSach Selected
        {
            set { selected = value; }
            get
            {
                if (selected == null)
                    selected = new BizSach();
                return selected;
            }
        }
        List<BizSach> sachs = new List<BizSach>();
        List<BizChuDe> chudes = new List<BizChuDe>();
        public frmChon()
        {
            InitializeComponent();
           
        }

        private void binding()
        {

            sachs = BizSach.GetAll();
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = sachs;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            string sach = textBox1.Text;
            int IDchude = Convert.ToInt32(comboBox1.SelectedValue.ToString());
            int giatu = 0;
            try { giatu = Convert.ToInt32(textBox2.Text); }
            catch { }
            int giaden = Int32.MaxValue;
            try { giaden = Convert.ToInt32(textBox3.Text); }
            catch { }
            bool dangban = checkBox1.Checked;
            sachs = BizSach.Find(sach, IDchude, giatu, giaden, dangban);
            dataGridView1.DataSource = null;
            dataGridView1.DataSource = sachs;
        }

        private void frmChon_Load(object sender, EventArgs e)
        {
            binding();
            chudes = BizChuDe.GetAll();
            BizChuDe tatca = new BizChuDe(0, "Tất cả");
            chudes.Add(tatca);
            comboBox1.DataSource = chudes;
            comboBox1.DisplayMember = "TenChuDe";
            comboBox1.ValueMember = "MaChuDe";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            binding();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            Selected = sachs[dataGridView1.SelectedRows[0].Index];
            this.Close();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Selected = new BizSach();
            this.Close();
        }

        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            Selected = sachs[dataGridView1.SelectedRows[0].Index];
            this.Close();
        }
    }
}
